<?php
function imagendePost($contenido,$post_id){
	global $post;
	$args = array(
	    	    'post_type' => 'attachment',  
	    	    'post_mime_type' => 'image',  
	    	    'numberposts' => -1,  
	    	        'order' => 'ASC',  
	    	    'post_status' => null,  
	    	    'post_parent' => $post_id  
	);	
	$attachments = get_posts($args);
	if ($attachments) {
		foreach ($attachments as $attachment) {
			return wp_get_attachment_thumb_url( $attachment->ID );
		}
		//Display image
	}else{
		return  get_bloginfo("template_url")."/images/header.png";
	}
}
function allPostImages($post_id){
	global $post;
	if(!$post_id)$post_id=$post->ID;
	$args = array(
	    	    'post_type' => 'attachment',  
	    	    'post_mime_type' => 'image',  
	    	    'numberposts' => -1,  
	    	        'order' => 'ASC',  
	    	    'post_status' => null,  
	    	    'post_parent' => $post_id  
	);	
	$attachments = get_posts($args);
	$thumbs=array();
	if ($attachments) {
		foreach ($attachments as $attachment) {
			$thumbs[]=wp_get_attachment_thumb_url( $attachment->ID );
		}
		//Display image
		return $thumbs;
	}else{
		return  get_bloginfo("template_url")."/images/header.png";
	}
}
function imagenFulldePost($post_id){
	global $post;
	$args = array(
      	    	    'post_type' => 'attachment',  
      	    	    'post_mime_type' => 'image',  
      	    	    'numberposts' => -1,  
      	    	    'order' => 'ASC',  
      	    	    'post_status' => null,  
      	    	    'post_parent' => $post_id  
	);
	$attachments = get_posts($args);
	if ($attachments) {
		$imagen= wp_get_attachment_image_src( $attachments[0]->ID ,'full');
		return array($imagen[0],$attachments[0]->ID);
	}else{
	  return false;
	}
}

/* Custom pages */
define('RECOMENDAR',"recomienda");
function theme_rules($reglas) {
	$nuevasreglas = array();
 	$nuevasreglas['^'.RECOMENDAR.'/([^/]+)?$'] = 'index.php?'.RECOMENDAR.'=$matches[1]';
	return $nuevasreglas + $reglas;
}
function add_vars($vars){
	$vars[] = "first_name";
	$vars[] = "email";
	return $vars;
}
function add_redirect(){

	global $wp_query;
	if ($wp_query->query_vars[RECOMENDAR]){
		if (file_exists(TEMPLATEPATH . '/recomienda.php')){
			include(TEMPLATEPATH . '/recomienda.php');
			exit;
		}
	}

}
function clean_rules() {
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}
function get_recomienda_vars(){
	return $$wp_query->query_vars["first_name"];
}
add_filter('rewrite_rules_array','theme_rules');
add_filter('query_vars', 'add_vars');
add_action('template_redirect','add_redirect');
add_action('init','clean_rules');


?>
